/*
 Copyright (c) 2013 MeherTJ G. All rights reserved.
 License: LGPL for personnal study or free software.
 */
#include "zen_app.h"

#if ZEN_OS_IOS && ZEN_GL

#import "ZenGLViewController.h"
#import "zen_app_ios.h"

#define runtime AppIOS::Instance()

@interface ZenGLViewController ()
@property (strong, nonatomic) EAGLContext *context;
@end

@implementation ZenGLViewController
{
//	GLKView *_view;
	bool _launched;
	int _draw_jump;
}

- (void)viewDidLoad {
	[super viewDidLoad];

	self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
	[EAGLContext setCurrentContext:self.context];

	GLKView *view = (GLKView *)self.view;

	view.context = self.context;
	view.drawableDepthFormat = GLKViewDrawableDepthFormat24;

	_launched = false;

	[self checkGLSupporting];
}
- (void)checkGLSupporting {
	NSLog(@"Apple Opengl support %s", glGetString(GL_EXTENSIONS));
}

- (CGSize) toPhysicalSize:(CGSize)size
{
	CGFloat scale = UIScreen.mainScreen.nativeScale;
	return { size.width * scale, size.height * scale };
}

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];

	CGSize rsize = [self toPhysicalSize:self.view.bounds.size];

	if(!_launched)
	{
		runtime->launch({(int)rsize.width, (int)rsize.height});
		_launched = true;
		_draw_jump = 0;

		runtime->Update();
	}
}

- (void)dealloc
{
}

- (void)Update
{
	if(_launched)
		runtime->Update();
}

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
	if(_launched)
	{
		runtime->draw();
	}
}

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
{
	if(runtime->IsRotatable()) return;

	CGSize rsize = [self toPhysicalSize:size];
	runtime->resize({(int)rsize.width, (int)rsize.height});

	[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}


- (Zen::AppTouch) toAppTouch:(UITouch*)uiTouch
{
	Zen::AppTouch touch;

	CGPoint pos = [uiTouch locationInView:self.view];
	CGSize size = self.view.bounds.size;

//	printf(">> %.2f %.2f\n", pos.x, pos.y);

	touch.x = pos.x / size.width;
	touch.y = 1 - pos.y / size.height;
	touch.no = (uint32_t)(long)uiTouch;
	return touch;
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	for (UITouch * uiTouch in touches)
	{
		runtime->touchDown([self toAppTouch:uiTouch]);
	}
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
	for (UITouch * uiTouch in touches)
	{
		runtime->touchMove([self toAppTouch:uiTouch]);
	}
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	for (UITouch * uiTouch in touches)
	{
		runtime->touchUp([self toAppTouch:uiTouch]);
	}
}
- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
	for (UITouch * uiTouch in touches)
	{
		runtime->touchCancel([self toAppTouch:uiTouch]);
	}
}

- (BOOL) shouldAutorotate
{
	return runtime->IsRotatable();
}

- (BOOL) prefersStatusBarHidden
{
	return !runtime->IsStatusVisible();
}

- (void)SetStatusVisible:(bool)flag {
	[self setNeedsStatusBarAppearanceUpdate];
}


- (void)onFPSChange {
	self.preferredFramesPerSecond = runtime->GetFramesPerSecond();
}

- (void)onMultiTouchChange {
	self.view.multipleTouchEnabled = runtime->IsMultiTouchEnabled();
}

- (void)onRotatableChange {
}

- (void)onStatusVisibleChange {
	[self setNeedsStatusBarAppearanceUpdate];
}

@end

#endif
